package client;

import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;

/**
 * This is RLM Windows auto-start mechanism.
 * 
 * The basic idea behind this mechanism is adding/deleting appropriate registry value to
 * "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" registry key accordingly
 * to user's choice.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */

/*
 * Modified by Ivan
 * PRIVREMENO, samo da može da se startuje na linuxu...
 */
public final class RLMAutostart {

	private static final String startupKeyName = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
	
	private static final String valueName = "RLMStartup";
	
	private static RegistryKey key = null;
	
	private static boolean isAutoStart = false;
	
	
	private RLMAutostart() {	}
	
	
	/**
	 * This methos opens startup registry key for writting access.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private static void openKeyWritting() {
		try {
		    if (System.getProperty("os.name").startsWith("Windows")) {
		        key = Registry.HKEY_CURRENT_USER.openSubKey(startupKeyName, RegistryKey.ACCESS_WRITE);
		    }
		}
		catch (NoSuchKeyException nske) {
//			nske.printStackTrace();
		}
		catch (RegistryException re) {
//			re.printStackTrace();
		}
	}
	
	
	/**
	 * This methos opens startup registry key for reading access.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private static void openKeyReading() {
		try {
		    if (System.getProperty("os.name").startsWith("Windows")) {
		        key = Registry.HKEY_CURRENT_USER.openSubKey(startupKeyName, RegistryKey.ACCESS_READ);
		    }
		}
		catch (NoSuchKeyException nske) {
//			nske.printStackTrace();
		}
		catch (RegistryException re) {
//			re.printStackTrace();
		}
	}
	
	
	/**
	 * This method tries to add registry startup value of RLM.exe file with absolute path,
	 * which represents RLM running folder.
	 * According to success of this operation, this method sets isAutoStart value, which
	 * represents auto-start flag.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public static void addToStartup() {
	    
	    if (System.getProperty("os.name").startsWith("Windows")) {
		
    		openKeyWritting();
    		
    		String valueData = System.getProperty("user.dir");
    		valueData += "\\RLM.exe" + " " + ClientApplication.CmdLineParams.auto_started.name() + "=true";
    		
    		RegStringValue value = new RegStringValue(key, valueName, RegistryValue.REG_SZ);
    		value.setData(valueData);
    		
    		try {
    			key.setValue(value);
    			isAutoStart = true;
    		}
    		catch (RegistryException re) {
    //			re.printStackTrace();
    			isAutoStart = false;
    		}
	    }
	}
	

	/**
	 * This mehtod tries to remove registry startup key.
	 * According to success of this operation, this method sets isAutoStart value, which
	 * represents auto-start flag.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public static void removeFromStartup() {
	    
	    if (System.getProperty("os.name").startsWith("Windows")) {
		
    		openKeyWritting();
    		
    		try {
    			key.deleteValue(valueName);
    			isAutoStart = false;
    		}
    		catch (RegistryException re) {
    //			re.printStackTrace();
    			isAutoStart = false;
    		}
	    }
	}
	
	
	
	/**
	 * This method checks is auto-startap value already exist.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public static void checkAutoStartup() {
	    
	    if (System.getProperty("os.name").startsWith("Windows")) {
		
    		openKeyReading();
    		
    		try {
    			RegistryValue value = key.getValue(valueName);
    			if (value != null) {
    				isAutoStart = true;
    				return;
    			}
    		}
    		catch (RegistryException re) {
    //			re.printStackTrace();
    			isAutoStart = false;
    		}
    		
    		isAutoStart = false;
	    }
	}
	
	
	
	/**
	 * This method returns auto-start flag value.
	 * @return true, if auto-start is activated, otherwise false
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public static boolean isAutoStart() {
		return isAutoStart;
	}

}
